package top.go2do.tagCategory.business.tag.controller;

import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import top.go2do.common.controller.TomatoController;
import top.go2do.common.module.TagPO;
import top.go2do.common.module.UserInfoBO;
import top.go2do.common.module.pageQuery.PageResult;
import top.go2do.tagCategory.business.tag.module.*;
import top.go2do.tagCategory.business.tag.service.TagService;

import java.util.List;

/**
 * @Description
 * @Author xingyj
 * @CreateTime 2025/3/28 17:28
 */
@RequestMapping("/tags")
@RestController
@Slf4j
public class TagController implements TomatoController<TagPO, TagCreateForm, TagModifyForm, Long, TagQueryCondition, TagQueryAllCondition, TagListVO> {

    @Autowired
    private TagService tagService;

    @Override
    public TagPO create(TagCreateForm createForm, UserInfoBO userInfo) {
        log.info("tags create appId:{}, tagName:{}", userInfo.getAppId(), createForm.getTagName());
        createForm.setAppId(userInfo.getAppId());
        createForm.setUserId(userInfo.getUserId());
        return tagService.create(createForm);
    }

    @Override
    public void modifyById(Long id, TagModifyForm modifyForm, UserInfoBO userInfo) {
        modifyForm.setTagId(id);
        modifyForm.setUserId(userInfo.getUserId());
        tagService.modifyById(modifyForm);
    }

    @Override
    public void removeById(Long id, UserInfoBO userInfo) {
        tagService.removeById(id);
    }

    @Override
    public TagPO getById(Long id) {
        return tagService.getById(id);
    }

    @Override
    public PageResult<TagListVO> myPageQuery(UserInfoBO userInfo, TagQueryCondition tagQueryCondition) {
        tagQueryCondition.setUserId(userInfo.getUserId());
        PageResult<TagPO> pageResult = tagService.pageQuery(tagQueryCondition);
        List<TagListVO> volist = tagService.po2vo(pageResult.getList());
        PageResult<TagListVO> finalResult = new PageResult<>();
        BeanUtils.copyProperties(pageResult, finalResult);
        finalResult.setList(volist);
        return finalResult;
    }

    @Override
    public List<TagListVO> myListAll(UserInfoBO userInfo, TagQueryAllCondition tagQueryAllCondition) {
        tagQueryAllCondition.setUserId(userInfo.getUserId());
        List<TagPO> poList = tagService.listAll(tagQueryAllCondition);
        List<TagListVO> volist = tagService.po2vo(poList);
        return volist;
    }
}

